/*****************************************************************************
*
*  PROJECT:		
*               
*  LICENSE:		
*
*  DEVELOPER:	
*
*****************************************************************************/

#include "CGame.h"

int CGame::m_nReturnValue;

HWND CGame::hWnd;
HDC CGame::hDC;

Eon::Skybox CGame::m_Skybox;
Eon::Camera CGame::m_Camera;
Eon::Model::modelMD5 CGame::m_Player;
Eon::Model::model3DS CGame::m_Player2;
Eon::Model::model3DS CGame::m_Player3;
Eon::TextureMgr CGame::m_TextureMgr;
Eon::Texture *CGame::m_TextureGrey_BMP;
Eon::Texture *CGame::m_TextureAbe_BMP;
Eon::Texture *CGame::m_TextureEon_BMP;
Eon::EffectsMgr CGame::m_EffectsMgr;

DWORD CGame::m_dwCurrentTime = 0;
DWORD CGame::m_dwFPSLimit = DEFAULT_FPS_LIMIT;
DWORD CGame::m_dwTimeOfLastFrame = 0;

int setting = 0;

int CGame::init( HDC hDC )
{
	GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat mat_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat mat_shininess[] = { 50.0f };
	GLfloat light_position[] = { 100.0f, 1.0f, 100.0f, 1.0f };

	glOrtho( -1280.0f, 1280.0f, -720.0f, 720.0f, 1.0f, -1.0f );
	glShadeModel( GL_SMOOTH );

	/*glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
	glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );
	glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
	glLightfv( GL_LIGHT0, GL_AMBIENT, light_position );*/

	//glEnable( GL_LIGHTING );
	//glEnable( GL_LIGHT0 );
	glEnable( GL_DEPTH_TEST );

	m_TextureGrey_BMP = m_TextureMgr.loadTexture( "textures/grey.bmp" );
	m_TextureAbe_BMP = m_TextureMgr.loadTexture( "textures/abe.bmp" );
	m_TextureEon_BMP = m_TextureMgr.loadTexture( "textures/eon.bmp" );

	m_Skybox.init();
	m_Camera.init( Eon::Vector3( 50.0f, 0.0f, 20.0f ), Eon::Vector3( 0.0f, 270.0f, 0.0f ) );
	m_Player.init( Eon::Vector3( 0.0f, -20.0f, 0.0f ), Eon::Vector3( 270.0f, 0.0f, 0.0f ), 0.2f, "models/qshambler.md5mesh" );
	m_Player3.init( Eon::Vector3( 0.0f, -10.0f, -30.0f ), Eon::Vector3( 270.0f, 0.0f, 270.0f), 1.0f, "models/cube.3ds" );
	//m_Player2.init( Eon::Vector3( 0.0f, -10.0f, -30.0f ), Eon::Vector3( 270.0f, 0.0f, 270.0f), 0.2f, "models/r8.3ds" );
	m_EffectsMgr.init();

	return TRUE;
}

void CGame::keyboardFunc( void )
{
	if ( GetAsyncKeyState( VK_W ) )
	{
		m_Camera.moveForward( CAMERA_MOVEMENT_SPEED );
	}

	if ( GetAsyncKeyState( VK_S ) )
	{
		m_Camera.moveBackward( CAMERA_MOVEMENT_SPEED );
	}

	if ( GetAsyncKeyState( VK_A ) )
	{
		m_Camera.rotateLeft( 1.0f );
	}

	if ( GetAsyncKeyState( VK_D ) )
	{
		m_Camera.rotateRight( 1.0f );
	}

	if ( GetAsyncKeyState( VK_Q ) )
	{
		m_Camera.moveLeft( CAMERA_MOVEMENT_SPEED );
	}

	if ( GetAsyncKeyState( VK_E ) )
	{
		m_Camera.moveRight( CAMERA_MOVEMENT_SPEED );
	}

	if ( GetAsyncKeyState( VK_O ) )
	{
		m_Camera.moveUp( 0.1f );
	}

	if ( GetAsyncKeyState( VK_L ) )
	{
		m_Camera.moveDown( 0.1f );
	}

	if ( GetAsyncKeyState( VK_ESC ) )
	{
		m_nReturnValue = RESULT_EXIT_GAME;
	}

	if ( GetAsyncKeyState( VK_M ) )
	{
		setting = 1;
	}

	if ( GetAsyncKeyState( VK_N ) )
	{
		setting = 0;
	}
}

int CGame::mainLoop( void )
{
	m_dwCurrentTime = timeGetTime();

	if ( ( m_dwCurrentTime - m_dwTimeOfLastFrame ) < ( 1000 / m_dwFPSLimit ) )
	{
		return m_nReturnValue;
	}

	m_dwTimeOfLastFrame = m_dwCurrentTime;

	keyboardFunc();

	return m_nReturnValue;
}

void CGame::renderGLScene( void )
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );

	glLoadIdentity();

	glClearColor( 1.0, 1.0, 1.0, 1.0 );

	m_Skybox.render( m_Camera );

	m_Camera.update();

	if ( setting == 1 )
	{
		//m_EffectsMgr.enableChromaticDispersion( m_Skybox, m_Camera, m_Player3, 1.0f, 0.5f, 0.5f, 0.5f );
		//m_EffectsMgr.enableRefractiveEnvironmentMapping( m_Skybox, m_Camera, m_Player3, 1.0f, 0.5f, m_TextureGrey_BMP->getID() );
		//m_EffectsMgr.enableReflectiveEnvironmentMapping( m_Skybox, m_Camera, m_Player, 1.0f, m_TextureGrey_BMP->getID() );
		m_EffectsMgr.enablePerFragmentLighting(	m_Camera, m_Player3, Eon::Vector3( 50.0f, 0.0f, 50.0f ), Eon::Vector3( 1.0f, 0.0f, 0.0f ), Eon::Vector3( 0.1f, 0.1f, 0.1f ),
												Eon::Vector3( 0.1f, 0.1f, 0.1f ), Eon::Vector3( 0.2f, 0.2f, 0.2f ), Eon::Vector3( 0.3f, 0.3f, 0.3f ), Eon::Vector3( 0.4f, 0.4f, 0.4f ), 1.0f );
		
		//m_Player.renderModel();
		
		m_TextureEon_BMP->bindTexture();
		GLUquadricObj *quadric;
		quadric = gluNewQuadric();
		gluQuadricNormals(quadric, GLU_SMOOTH);
		gluQuadricTexture(quadric, GL_TRUE);
		glEnable(GL_TEXTURE_2D);
		gluSphere(quadric,20,10,10);

		m_EffectsMgr.disableShader();
		//m_EffectsMgr.disableReflectiveEnvironmentMapping();
		//m_EffectsMgr.disableRefractiveEnvironmentMapping();
		//m_EffectsMgr.disableChromaticDispersion();
	}
	else
	{
		//m_EffectsMgr.enableChromaticDispersion( m_Skybox, m_Camera, m_Player3, 1.0f, 0.5f, 0.5f, 0.5f );
		//m_EffectsMgr.enableRefractiveEnvironmentMapping( m_Skybox, m_Camera, m_Player, 1.0f, 0.5f, m_TextureEon_BMP->getID() );
		//m_EffectsMgr.enableReflectiveEnvironmentMapping( m_Skybox, m_Camera, m_Player, 1.0f, m_TextureEon_BMP->getID() );
		m_EffectsMgr.enablePerVertexLighting(	m_Camera, m_Player3, Eon::Vector3( 50.0f, 0.0f, 50.0f ), Eon::Vector3( 1.0f, 0.0f, 0.0f ), Eon::Vector3( 0.1f, 0.1f, 0.1f ),
												Eon::Vector3( 0.1f, 0.1f, 0.1f ), Eon::Vector3( 0.2f, 0.2f, 0.2f ), Eon::Vector3( 0.3f, 0.3f, 0.3f ), Eon::Vector3( 0.4f, 0.4f, 0.4f ), 1.0f );

		m_TextureEon_BMP->bindTexture();
		GLUquadricObj *quadric;
		quadric = gluNewQuadric();
		gluQuadricNormals(quadric, GLU_SMOOTH);
		gluQuadricTexture(quadric, GL_TRUE);
		glEnable(GL_TEXTURE_2D);
		gluSphere(quadric,20,10,10);

		m_EffectsMgr.disableShader();
		//m_EffectsMgr.disableRefractiveEnvironmentMapping();
		//m_EffectsMgr.disableChromaticDispersion();
	}
}

void CGame::resizeGLScene ( GLsizei w, GLsizei h )
{
	glViewport ( 0, 0, (GLsizei) w, (GLsizei) h );
	glMatrixMode ( GL_PROJECTION );
	glLoadIdentity ();
	gluPerspective ( 60.0f, (GLfloat)w/(GLfloat)h, 0.1f, 1000024.0f );
	glMatrixMode ( GL_MODELVIEW );
	glLoadIdentity ();
}

void CGame::shutDown( void )
{
	m_EffectsMgr.destroy();
}